<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller\playlet;

use App\Common\Response\SystemCode;
use App\Controller\BaseController;
use App\Controller\playlet\AbstractController;
use App\Service\playlet\StsService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;

#[AutoController]
class StsController extends BaseController
{
    #[Inject]
    private StsService $stsService;

    public function index(RequestInterface $request, ResponseInterface $response)
    {
        return false;
        //return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功',$res);
    }
    public function uploadPath(RequestInterface $request, ResponseInterface $response)
    {

        $res = $this->stsService->getStsToken();
        $type = $request->input('type','videos');
        $suffix = $request->input('suffix','');
        if($type =='videos') {
            $type = 'videos';
        } elseif($type == 'images') {
            $type = 'images';
        } else {
            return $this->response(SystemCode::SYSTEM_ERROR_PARAMETER, '缺少参数',[]);
        }

        $saveFilename = md5(uniqid());

        // md5前缀目录
        $prefixDir = substr($saveFilename, 0, 2);
        $path = "/web/$type/{$prefixDir}/";
        $data['token'] = $res;
        $data['file_path'] = $path;
        if(!empty($suffix)){
            $data['file_name'] = $saveFilename.$suffix;
        }else{
            $data['file_name'] = $saveFilename;
        }



        return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功',$data);
    }

}
